//
//  PopupPickerView.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-6-25.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "PopupPickerView.h"

#define kPickerAnimationDuration    0.40   // duration for the animation to slide the date picker into view
#define kViewHeight     206

@interface PopupPickerView()

@end

@implementation PopupPickerView

- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"initWithFrame");
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    NSLog(@"initWithCoder %@", aDecoder);
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

-(id)initWithPickerDataSource:(id)pickerDataSource pickerDelegate:(id)pickerDelegate{
    NSLog(@"init");
    self = [super initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kViewHeight)];
    if (self) {
        self.backgroundColor = RGBACOLOR(232, 232, 232, 1.0);
        
        self.toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        UIBarButtonItem *done = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeView:)];
        [self.toolbar setItems:@[done]];
//        [self addSubview:self.toolbar];
        
        self.picker = [[UIPickerView alloc]init];
//        self.picker.backgroundColor = [UIColor whiteColor];
        self.picker.dataSource = pickerDataSource;
        self.picker.delegate = pickerDelegate;
        [self addSubview:self.picker];
        
        [self addSubview:self.toolbar];
    }
    return self;
}

-(void)showPickerView:(UIView *)superView{
    NSLog(@"show picker view");
    [superView addSubview:self];
    CGRect endFrame = CGRectMake(0, SCREEN_HEIGHT-kViewHeight, SCREEN_WIDTH, kViewHeight);
    [UIView animateWithDuration:kPickerAnimationDuration animations: ^{ self.frame = endFrame; }
                     completion:nil];
}

-(void)closeView:(id)sender{
    NSLog(@"close picker view");
    CGRect endFrame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kViewHeight);
    [UIView animateWithDuration:kPickerAnimationDuration animations: ^{ self.frame = endFrame; }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
